package xcmg.device.web.purchase;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.service.purchase.PurchasePlanService;
import xcmg.device.service.sale.SaleIntentionService;
import xcmg.device.service.vo.purchase.PurchasePlanVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Date;

/**
 * Created by wy on 2018/5/14.
 */
@RestController
@RequestMapping("/purchasePlan")
public class PurchasePlanController extends BasicController {

    @Autowired
    private SaleIntentionService saleIntentionService;

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Autowired
    private PurchasePlanService purchasePlanService;


    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation("查询采购计划列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<PurchasePlanVO> condition)throws Exception{
        PurchasePlanVO purchasePlanVO =condition.getFilters();
        if(purchasePlanVO == null){
            purchasePlanVO = new PurchasePlanVO();
        }
        purchasePlanVO.setOrgId(getOrgId());
        condition.setFilters(purchasePlanVO);
        PageCommonVO pageCommonVO= purchasePlanService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public void test() throws Exception{
        for (String orgId : saleIntentionService.listOrgIds()) {
            purchasePlanService.saveOrUpdatePurchasePlan(orgId);
        }
    }

    /*
     * 定时任务同步采购计划数据
     *
     * @return void
     * @Scheduled(cron = "0 0 0/1 * * ? ") // 每一小时执行一次
     * */
//    @Scheduled(fixedRate = 10000)//每5秒执行一次
//    @Scheduled(cron = "0 30 6 * * ? ")
//    @SchedulerLock(name = "scheduledTaskName15", lockAtMostFor = 14*60*1000, lockAtLeastFor = 14*60*1000)
    public void syncPurchasePlan() throws Exception {
        System.out.println("执行Quartz定时器任务："+new Date());
        TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try {

            for (String orgId : saleIntentionService.listOrgIds()) {
                purchasePlanService.saveOrUpdatePurchasePlan(orgId);
            }

            transactionManager.rollback(status);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void exportAll(PurchasePlanVO condition)throws Exception {
        condition.setOrgId(getOrgId());
        purchasePlanService.exportAll(condition);
    }
}
